package com.lx.chat.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lx.bilibili.entity.Message;
import com.lx.bilibili.entity.MessageEx;
import com.lx.chat.dao.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2025-03-26
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    MessageMapper messageMapper;

    @RequestMapping("saveChatMessage")
    public void saveChatMessage(@RequestBody Message message){
        message.setTime(new Timestamp(System.currentTimeMillis()).toLocalDateTime());
        messageMapper.insert(message);
    }

    @RequestMapping("getChatMessage")
    public List<MessageEx> getChatMessage(int id, int recvId){
        return messageMapper.getChatMessage(id,recvId);
    }

    @RequestMapping("updataMessageState")
    public void updataMessageState(int uid, int recvId){
        LambdaUpdateWrapper<Message> luw = new LambdaUpdateWrapper<>();
        luw.set(Message::getState, 1);
        luw.eq(Message::getUid,uid);
        luw.eq(Message::getRecvid, recvId);
        luw.eq(Message::getState,0);
        messageMapper.update(luw);

    }

}
